package com.gxa.modules.service.drivingManagementService.Impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gxa.common.base.RespResult;
import com.gxa.modules.dto.drivingManagement.CarInfoDTO;
import com.gxa.modules.dto.drivingManagement.QueryCarInfoDTO;
import com.gxa.modules.entity.CarInfo;
import com.gxa.modules.mapper.drivingManagement.CarInfoMapper;
import com.gxa.modules.service.drivingManagementService.CarInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;


@Service
@Slf4j
public class CarInfoServiceImpl implements CarInfoService {//车辆管理

    @Autowired
    private CarInfoMapper carInfoMapper;


    @Override
    public RespResult addCar(CarInfo carInfo){//添加车辆
        int result = carInfoMapper.insert(carInfo);

        if(result>0){
            return RespResult.success();
        }else {
            return RespResult.error("添加失败！");
        }
    }

    @Override
    public RespResult queryCarInfo(QueryCarInfoDTO queryCarInfoDTO) {//查询全部车辆

        Page<CarInfo> carInfoPage = new Page<>(queryCarInfoDTO.getPage(),queryCarInfoDTO.getLimit());

        QueryWrapper<CarInfo> queryWrapper = new QueryWrapper();
        queryWrapper.eq(StringUtils.hasText(queryCarInfoDTO.getCarNumber()),"car_number",queryCarInfoDTO.getCarNumber()).or()
                    .eq(StringUtils.hasText(queryCarInfoDTO.getVehicleType()),"vehicle_type",queryCarInfoDTO.getVehicleType()).or()
                    .eq(StringUtils.hasText(queryCarInfoDTO.getCarType()),"car_type",queryCarInfoDTO.getCarType()).or()
                    .eq(StringUtils.hasText((CharSequence) queryCarInfoDTO.getBuyTime()),"buy_time",queryCarInfoDTO.getBuyTime()).or()
                    .eq(StringUtils.hasText((CharSequence) queryCarInfoDTO.getInsuranceTime()),"insurance_time",queryCarInfoDTO.getInsuranceTime()).or()
                    .eq(StringUtils.hasText((CharSequence) queryCarInfoDTO.getAnnualAudi()),"annual_audit",queryCarInfoDTO.getAnnualAudi());


        try{
            Page<CarInfo> carInfoList = carInfoMapper.selectPage(carInfoPage, queryWrapper);

            return RespResult.success(carInfoList);

        }catch (Exception e){
            e.printStackTrace();
            return RespResult.error("查询失败!");
        }
    }

    @Override
    public RespResult queryCarByCarNumberOrLicensePlate(Integer carNumber, String licensePlate) {//根据车辆编号或者车牌号查询车辆信息

        QueryWrapper<CarInfo> queryWrapper= new QueryWrapper<>();
        queryWrapper.eq(StringUtils.hasText(String.valueOf(carNumber)),"car_number", carNumber).or()
                    .eq(StringUtils.hasText(licensePlate),"license_plate", licensePlate);

        try{
            CarInfo carInfo =  carInfoMapper.selectOne(queryWrapper);
            return RespResult.success(carInfo);
        }catch (Exception e){
            e.printStackTrace();
            return RespResult.error("查询失败!");
        }
    }

    @Override
    public RespResult updateCar(CarInfo carInfo) {//更新车辆信息
        int result = carInfoMapper.updateById(carInfo);

        if(result>0){
            return RespResult.success();
        }else {
            return RespResult.error("更新失败！");
        }
    }
}
